package com.example.a1.myapplication.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.a1.myapplication.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 郭晓龙
 * @time on 2019/6/12
 * @email xiaolong.guo@inossem.com
 */
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {

    private Context context;
    private List<String> list = new ArrayList<>();

    public SearchAdapter(Context context) {
        this.context = context;
    }

    public void addList(List<String> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void clearList() {
        this.list.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_search_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.tvName.setText(list.get(i));
        if(i==0){
            viewHolder.llItem.setBackgroundResource(R.drawable.bg_item_blue_stroke);
            viewHolder.tvName.setTextColor(context.getResources().getColor(R.color.base_bg));
        }else{
            viewHolder.llItem.setBackgroundResource(R.drawable.bg_item_gray_stroke);
            viewHolder.tvName.setTextColor(context.getResources().getColor(R.color.base_black));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        LinearLayout llItem;

        ViewHolder(View itemView) {
            super(itemView);
            llItem = itemView.findViewById(R.id.llItem);
            tvName = itemView.findViewById(R.id.tvName);
        }
    }
}
